/**
 *******************************************************************************
 * @FileName  : l2cap_defs.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-02-18
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#ifndef L2CAP_DEFS_H_
#define L2CAP_DEFS_H_

#define L2CAP_PDU_HDR_LEN      4   /*!< L2CAP packet header length */
#define L2CAP_MIN_MTU          23  /*!< Minimum packet payload MTU for LE */
#define L2CAP_SIG_HDR_LEN      4   /*!< L2CAP signaling command header length */
#define L2CAP_LE_SDU_HDR_LEN   2   /*!< L2CAP LE SDU data header length */

/*!   Start of L2CAP in packet */
#define L2CAP_PAYLOAD_START       (HCI_ACL_HDR_LEN + L2CAP_PDU_HDR_LEN)

/*!   L2CAP signaling packet base length, */
#define L2CAP_SIG_PKT_BASE_LEN    (L2CAP_PDU_HDR_LEN + L2CAP_SIG_HDR_LEN)

/*!   L2CAP LE SDU packet base length */
#define L2CAP_LE_SDU_PKT_BASE_LEN  (L2CAP_PDU_HDR_LEN + L2C_LE_SDU_HDR_LEN)


/*!   L2CAP Parameter Lengths. Signaling packet parameter lengths. */
#define L2CAP_SIG_CONN_UPDATE_REQ_LEN          8   /*!< Connection update request length. */
#define L2CAP_SIG_CONN_UPDATE_RSP_LEN          2   /*!< Connection update response length. */
#define L2CAP_SIG_CMD_REJ_LEN                  2   /*!< Command reject length. */
#define L2CAP_SIG_DISCONN_REQ_LEN              4   /*!< Disconnection request length. */
#define L2CAP_SIG_DISCONN_RSP_LEN              4   /*!< Disconnection response length. */
#define L2CAP_SIG_LE_CREDIT_CONN_REQ_LEN       10  /*!< LE connection request length. */
#define L2CAP_SIG_LE_CREDIT_CONN_RSP_LEN       10  /*!< LE connection response length. */
#define L2CAP_SIG_FLOW_CTRL_CREDIT_LEN         4   /*!< Flow control credit lenghth. */

/*!   L2CAP Channel Id define. */
#define L2CAP_CID_ATT         (0x0004)
#define L2CAP_CID_LE_SIG      (0x0005)
#define L2CAP_CID_SMP         (0x0006)

/*!   L2CAP Signal(LE) Code define. */
#define L2CAP_SIG_CMD_REJECT                0x01 /*!< Comand reject.       */
#define L2CAP_SIG_DISCONNECT_REQ            0x06 /*!< Disconnect request.  */
#define L2CAP_SIG_DISCONNECT_RSP            0x07 /*!< Disconnect response. */
#define L2CAP_SIG_CONN_PARAM_UPDATE_REQ     0x12 /*!< Connection parameter update request.  */
#define L2CAP_SIG_CONN_PARAM_UPDATE_RSP     0x13 /*!< Connection parameter update response. */
#define L2CAP_SIG_LE_CREDIT_CONN_REQ        0x14 /*!< LE credit based connection request.   */
#define L2CAP_SIG_LE_CREDIT_CONN_RSP        0x15 /*!< LE credit based connection response.  */
#define L2CAP_SIG_FLOW_CTRL_CREDIT          0x16 /*!< LE flow control credit. */
//BLE 5.2 signal
#define L2CAP_SIG_CREDIT_CONN_REQ           0x17 /*!< Enhanced credit based connection request.   */
#define L2CAP_SIG_CREDIT_CONN_RSP           0x18 /*!< Enhanced credit based connection response.  */
#define L2CAP_SIG_CREDIT_RECONFIG_REQ       0x19 /*!< Enhanced credit based reconfigure request.  */
#define L2CAP_SIG_CREDIT_RFCONFIG_RSP       0x1A /*!< Enhanced credit based reconfigure response. */

/*!   L2cap command reject reason define. */
#define L2CAP_REJ_NOT_UNDERSTOOD        0x0000 /*!< Command not understood */
#define L2CAP_REJ_SIGNAL_MTU_EXCEEDED   0x0001 /*!< Signaling MTU exceeded */
#define L2CAP_REJ_INVALID_CID           0x0002 /*!< Invalid CID in request */

/** @name L2CAP Connection Parameter Update Result Codes
 *        BLE defined result codes
 */
/**@{*/
#define L2CAP_CONN_PARAM_ACCEPTED       0x0000    /*!< Connection parameters accepted */
#define L2CAP_CONN_PARAM_REJECTED       0x0001    /*!< Connection parameters rejected */
/**@}*/


/** @Name L2CAP Connection Result Codes
 * 		  BLE defined result codes
 *  @{
 */
#define L2CAP_RESULT_COC_CONN_SUCCESS               0x0000    /*!< Connection successful */
#define L2CAP_RESULT_COC_PSM_UNSUPPORTED            0x0002    /*!< Connection refused SPSM not supported */
#define L2CAP_RESULT_COC_NO_RESOURCES               0x0004    /*!< Connection refused no resources available */
#define L2CAP_RESULT_COC_INSUFFICIENT_AUTH          0x0005    /*!< Connection refused insufficient authentication */
#define L2CAP_RESULT_COC_INSUFFICIENT_AUTHORIZ      0x0006    /*!< Connection refused insufficient authorization */
#define L2CAP_RESULT_COC_INSUFFICIENT_ENC_KEY_SIZE  0x0007    /*!< Connection refused insufficient encryption key size */
#define L2CAP_RESULT_COC_INSUFFICIENT_ENC           0x0008    /*!< Connection Refused insufficient encryption */
#define L2CAP_RESULT_COC_INVALID_SCID               0x0009    /*!< Connection refused invalid source CID */
#define L2CAP_RESULT_COC_SCID_ALLOCATED             0x000A    /*!< Connection refused source CID already allocated */
#define L2CAP_RESULT_COC_UNACCEPT_PARAM             0x000B    /*!< Connection refused unacceptable parameters */
/**@}*/

/**
 * @Name L2CAP Interal Connection Result Codes
 *         Proprietary codes not sent in any L2CAP packet.
 * @{
 */
#define L2CAP_CONN_FAIL_TIMEOUT         0xF000    /*!< Request timeout */
/**@}*/

/** @name L2CAP Signaling Parameter Value Ranges
 *
 */
/**@{*/
#define L2CAP_PSM_MIN                   0x0001  /*!< PSM minimum. */
#define L2CAP_PSM_MAX                   0x00FF  /*!< PSM maximum. */
#define L2CAP_CID_DYN_MIN               0x0040  /*!< CID dynamic minimum. */
#define L2CAP_CID_DYN_MAX               0x007F  /*!< CID dynamic maximum. */
#define L2CAP_MTU_MIN                   0x0017  /*!< MTU minimum. */
#define L2CAP_MPS_MIN                   0x0017  /*!< MPS minimum. */
#define L2CAP_MPS_MAX                   0xFFFD  /*!< MPS maximum. */
#define L2CAP_CREDITS_MAX               0xFFFF  /*!< Credits maximum. */
/**@}*/


#define L2CAP_PSM_ATT                  0x001F  /*!< PSM for ATT */
#define L2CAP_PSM_EATT                 0x0027  /*!< PSM for EATT */

#endif /* L2CAP_DEF_H_ */
